<template>
  <div class="login-box">
    <div class="loginForm">
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <h3 class="title">账户登陆</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-s-goods" type="password" placeholder="密码" show-password @keyup.enter.native="handleLogin()"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button :loading="loading" type="primary" style="width: 100%; font-size:1rem;" @click.native.prevent="handleLogin()">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>

        <div class="login-help">
          <span class="help-link help-color"><a href="#/register">立即注册</a> </span>
          <span class="help-color">|</span>
          <span class="help-link help-color"><a href="#/retrievepwd">忘记密码？</a> </span>
        </div>

        <div class="login-other">
          <div class="soild_text">
            其他登陆方式
          </div>
          <div class="other-list">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-QQ1"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-weibo"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-zhifubao"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-weixin"></use>
            </svg>
          </div>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入用户名' },
          { min: 3, max: 8, message: '长度在3-8字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6-15字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (valid) {
          this.loading = true
          console.log(this.loginForm);
          let params = {}
          Object.assign(params,this.loginForm)
          params.password = this.$md5(params.password)
          const { data: res } = await this.$http.post('/user/login', params).catch(() => {
            return this.$message.error("密码与账号不一致")

          })
          console.log(res);
          if (res.code == 200) {
            window.sessionStorage.setItem('token', res.data.token)  // 保存token
            this.$message.success('登录成功！')
            this.$router.push('/index') // 跳转到home页
          } else {
            this.$message.error('登录失败，请检查用户名和密码！')
          }
        }
        this.loading = false
      })
    }
  }

}
</script>

<style lang="less" scoped>
.login-box {
  width: 100%;
  min-width: 1200px;
  height: calc(100% - 170px);
  min-height: 500px;
  background: url(../../assets/images/bg/login_bg.jpg) no-repeat 55% 0;
  position: relative;
  .loginForm {
    width: 370px;
    height: 430px;
    border-radius: 6px;
    background: rgba(255, 255, 255, 1);
    padding: 25px 25px 5px 25px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(45%, -55%);
    .title {
      margin: 0 auto 40px auto;
      text-align: center;
      color: #f10180;
      font-size: 1.4rem;
    }
    .login-help {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 0 5px;

      .help-color {
        font-size: 0.9rem;
        color: #999;
      }
      .help-link {
        padding: 0 5px;
        cursor: pointer;
        transition: all 0.3s;
      }
      .help-link:hover {
        color: #f10180;
      }
      a {
        text-decoration: none;
        color: inherit;
      }
    }
    .login-other {
      margin-top: 35px;
      .soild_text {
        position: relative;
        text-align: center;
        margin: 20px auto;
        height: 20px;
        font-size: 14px;
        color: #bbb;
        &::before,
        &::after {
          content: "";
          position: absolute;
          top: 50%;
          border: solid 0.5px #e0e0e0;
          width: 40%;
        }
        &::before {
          left: 0;
        }
        &::after {
          right: 0;
        }
      }
      .other-list {
        display: flex;
        justify-content: space-evenly;

        .icon {
          width: 2em;
          height: 2em;
          vertical-align: -0.15em;
          fill: currentColor;
          overflow: hidden;
          cursor: pointer;
        }
      }
    }
  }
}
</style>